package com.java_foundation.thread_demo.test_12;

/**
 * @program: java_foundation
 * @description: 多线程运行状态切换实例
 * @author: xiongbangwen <Email>xiongbangwen@163.com</Email>
 * @create: 2020-06-06 15:38
 **/
public class ThreadStatusDemo_1 {
    public static Thread thread;
    public static ThreadStatusDemo_1 obj;

    public static void main(String[] args) throws InterruptedException {
        //第一种状态切换： 新建 -> 运行 -> 终止
        System.out.println("======  第一种状态切换: 新建 -> 运行 -> 终止  ======");
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread1当前的状态： " + Thread.currentThread().getState().toString());
                System.out.println("thread1执行了" );
            }
        });
        System.out.println("没调用start方法，thread1当前状态： " + thread1.getState().toString());
        thread1.start();
        Thread.sleep(2000L);//等待thread1执行结束，再看状态
        System.out.println("等待两秒后，thread1当前状态：  " + thread1.getState().toString());
        //thread1.start(); 注意：线程终止之后，再进行调用会抛出IllegalThreadStateException异常

        System.out.println();
        System.out.println("------------------------------------------------------");
        System.out.println();
        //第二种状态切换： 新建 -> 运行 ->等待 ->运行 -> 终止 （sleep方式）
        System.out.println("======  第二种状态切换： 新建 -> 运行 ->等待 ->运行 -> 终止 （sleep方式）  ======");
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {//将线程2移动到等待状态，1.5s后自动唤醒
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread2当前的状态： " + Thread.currentThread().getState().toString());
                System.out.println("thread2执行了" );
            }
        });
        System.out.println("没调用start方法，thread2当前状态：" + thread2.getState().toString());
        thread2.start();
        System.out.println("调用了start方法，thread2当前状态：" + thread2.getState().toString());
        Thread.sleep(200L);//等待0.2秒再看状态
        System.out.println("等了0.2秒后，thread2状态：" + thread2.getState().toString());
        Thread.sleep(3000L);//等待3秒再看状态
        System.out.println("等了3秒后，thread2状态：" + thread2.getState().toString());

        System.out.println();
        System.out.println("------------------------------------------------------");
        System.out.println();
        //第三种状态切换： 新建 -> 运行 ->阻塞 ->运行 -> 终止
        System.out.println("======  第三种状态切换： 新建 -> 运行 ->阻塞 ->运行 -> 终止  ======");
        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (ThreadStatusDemo_1.class) {
                    System.out.println("thread3当前的状态： " + Thread.currentThread().getState().toString());
                    System.out.println("thread3执行了");
                }
            }
        });
        synchronized (ThreadStatusDemo_1.class) {
            System.out.println("没调用start方法，thread3当前状态：" + thread3.getState().toString());
            thread3.start();
            System.out.println("调用了start方法，thread3当前状态：" + thread3.getState().toString());
            Thread.sleep(200L);//等待0.2秒再看状态
            System.out.println("等了0.2秒后，thread3状态：" + thread3.getState().toString());
        }
        Thread.sleep(3000L);//等待3秒再看状态
        System.out.println("等了3秒后，thread3状态：" + thread3.getState().toString());

    }
}
